import { Body, Ctx, JsonController, Post } from 'routing-controllers'
import { Inject, Service } from 'typedi';
import { AuthService } from '@/services/AuthService';
import { LoginDto } from '@/dtos/AuthDto';
import { Context } from 'koa';

@Service()
@JsonController('/auth')
export class AuthController {

  @Inject()
  private authService!: AuthService;

  @Post('/login')
  public async login(@Body() loginDto: LoginDto, @Ctx() ctx: Context): Promise<any> {
    const user = await this.authService.login(loginDto);
    if (!user) {
      throw new Error('login failed');
    }
    ctx.session!.user = Array(10).fill(0).map(() => user);
    return user;
  }

}
